package com.xm.xmapp.customview;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import com.xm.xmapp.R;
import com.xm.xmapp.myinterface.TipInterFace;

public class QYTipDialog extends Dialog {

	private Context context;

	private TextView left, right;

	private TipInterFace in;
	private TextView content;
	private String title_str,conent_str;



	public QYTipDialog(Context context, String title,String content, TipInterFace in) {
		super(context, R.style.Dialog);
		// TODO Auto-generated constructor stub
		this.context = context;
		this.title_str = title;
		this.conent_str = content;
		this.in = in;
	
	}

	@SuppressLint("InflateParams")
	public void init() {
		LayoutInflater inflater = LayoutInflater.from(context);
		View view = inflater.inflate(R.layout.dialog_tip, null);
		TextView title = (TextView) view.findViewById(R.id.title);
		content = (TextView) view.findViewById(R.id.content);
		
		left = (TextView) view.findViewById(R.id.left);
		right = (TextView) view.findViewById(R.id.right);
		title.setText(title_str == null ? "" : title_str);
		content.setText(conent_str == null ? "" : conent_str);
		left.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				context =null;
				dismiss();
				if (in != null)
					in.cancle();
			}
		});

		right.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				context =null;
				dismiss();
				if (in != null)
					in.sure();

			}
		});

		setContentView(view);

		Window dialogWindow = getWindow();
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
		lp.width = (int) (d.widthPixels * 0.8); // 高度设置为屏幕的0.6
		dialogWindow.setAttributes(lp);
	}

	public void setLeft(String str) {
		if (str == null || str.equals(""))
			return;
		left.setText(str);

	}
	
	public void setRight(String str) {
		if (str == null || str.equals(""))
			return;
		right.setText(str);

	}
	
	public void setContent(String str) {
		if (str == null || str.equals(""))
			return;
		content.setText(str);

	}

	@Override
	public void show() {
		// TODO Auto-generated method stub
		super.show();
		/** 3.自动弹出软键盘 **/

		content.setFocusable(true);
		content.setFocusableInTouchMode(true);
		content.requestFocus();
		InputMethodManager inputManager = (InputMethodManager) content
				.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
		inputManager.showSoftInput(content, 0);

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		init();

	}
}
